package com.simoniu.statement.callablestatementdemo;

import com.simoniu.util.DBUtil;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;

public class CallableStatementDemo {

    public void executeSqlProcedure(){
        Connection conn = null;
        CallableStatement cstmt = null;
        String sql = "";
        try{
            conn = DBUtil.getConnection();
            sql = "{call sumCoursePassNumber(?,?)}";
            cstmt = conn.prepareCall(sql);
            //传参
            cstmt.setString(1, "C001");
            cstmt.registerOutParameter(2, Types.NUMERIC);
            //执行存储过程
            cstmt.execute();
            System.out.println("C001这门课程及格人数是："+cstmt.getInt(2));

        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            try {
                if (cstmt != null) {
                    cstmt.close();
                    cstmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        CallableStatementDemo demo = new CallableStatementDemo();
        demo.executeSqlProcedure();
    }
}
